<template>
  <div class="wrapper">
      <img src="http://www.dell-lee.com/imgs/vue3/user.png" class="wrapper__img" />
      <div class="wrapper__input">
        <input
        class="wrapper__input__content"
        placeholder="请输入用户名"
        v-model="data.username"
        />
      </div>
      <div class="wrapper__input">
        <input
        type="password"
        class="wrapper__input__content"
        placeholder="请输入密码"
        v-model="data.password"
        />
      </div>
      <div class="wrapper__login-button" @click="handleLogin">登录</div>
      <div class="wrapper__login-link" @click="handleRegisterClick">立即注册</div>
  </div>
</template>

<script>
import { useRouter } from 'vue-router'
import axios from 'axios'
import { reactive } from 'vue'

axios.defaults.headers.post['Content-Type'] = 'application/json'

export default {
  name: 'Login',
  setup () {
    const data = reactive({
      username: '',
      password: ''
    })
    const router = useRouter()
    // 写法二
    const handleLogin = async () => {
      try {
        const result = await axios.post('https://www.fastmock.site/mock/ec090b403df8ff463c06b52cca53a6f7/jd/api/user/login', {
          username: data.username,
          password: data.password
        })
        if (result?.data?.errno === 0) {
          // localStorage本地存储
          localStorage.isLogin = true
          router.push({ name: 'Home' }) // 页面跳转
        } else {
          alert('登录失败')
        }
      } catch (e) {
        alert('请求失败')
      }
    }
    /* 写法一
    const handleLogin = () => {
      axios.post('https://www.fastmock.site/mock/ec090b403df8ff463c06b52cca53a6f7/jd/api/user/login', {
        username: data.username,
        password: data.password
      }).then(() => {
        // localStorage本地存储
        localStorage.isLogin = true
        router.push({ name: 'Home' }) // 页面跳转
      }).catch(() => {
        alert('登录失败')
      })
    }
    */
    const handleRegisterClick = () => {
      router.push({ name: 'Register' })
    }
    return { handleLogin, handleRegisterClick, data }
  }
}
</script>

<style lang="scss" scoped>
@import '../../style/viriables.scss';
.wrapper {
  position: absolute;
  top: 50%;
  left: 0;
  right: 0;
  transform: translateY(-50%); // 这个元素里高度的50%
  &__img {
    display: block;
    width: .66rem;
    height: .66rem;
    margin: 0 auto .4rem auto;
  }
  &__input {
    height: .48rem;
    margin: 0 .4rem .16rem .4rem;
    padding: 0 .16rem;
    background: #F9F9F9;
    border: .01rem solid rgba(0,0,0,0.10);
    border-radius: .06rem;
    &__content {
      line-height: .48rem;
      border: none;
      outline: none;
      width: 100%;
      background: none;
      font-size: .16rem;
      color: $content-notice-fontcolor;
      &::placeholder { // 修饰符
        color: $content-notice-fontcolor;
      }
    }
  }
  &__login-button {
    margin: .32rem .4rem .16rem .4rem;
    line-height: .48rem;
    background: #0091FF;
    box-shadow: 0 .04rem .08rem 0 rgba(0,145,255,0.32); // 素添加阴影：
    border-radius: .04rem;
    color: #fff;
    font-size: .16rem;
    text-align: center;
  }
  &__login-link {
    text-align: center;
    font-size: .14rem;
    color: $content-notice-fontcolor;
  }
}
</style>
